/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import database_beans.Katedra;
import database_beans.Predmet;
import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import server.Util;

/**
 *
 * @author stefan
 */
@ManagedBean(name = "adminCtrl")
@ViewScoped
public class AdminPredmetControler implements Serializable {

    private Predmet noviPredmet;

    private ArrayList<Katedra> katedreList;

    private ArrayList<Predmet> predmetList;

    private String selectedKatedraString;

    private Katedra selectedKatedra;

    private String selectedPredmetString;

    private Predmet selectedPredmet;

    private boolean imaKatedra;

    private boolean imaPredmet;

    public AdminPredmetControler() {

    }

    @PostConstruct
    public void init() {
        loadKatedre();
        loadPredmeti();
        noviPredmet = new Predmet();
    }

    private void loadKatedre() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria c = session.createCriteria(Katedra.class);
            katedreList = (ArrayList<Katedra>) c.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
        }
    }

    public void onChangeKatedra() {
        selectedKatedra = new Katedra();
        imaKatedra = false;
        if (!"".equals(selectedKatedraString)) {
            for (Katedra kat : katedreList) {
                if (selectedKatedraString.equals(kat.getOpis())) {
                    selectedKatedra = kat;
                    imaKatedra = true;
                }
            }
        }
    }

    public void snimiPredmet() {

        if (imaKatedra) {
            Session session = Util.getSesion();
            Transaction t = session.beginTransaction();
            try {
                noviPredmet.setKatedra(selectedKatedra);
                noviPredmet.setInformacije(" ");
                session.save(noviPredmet);
                t.commit();
                noviPredmet = new Predmet();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Predmet snimljen"));
            } catch (Exception e) {
                t.rollback();
                e.printStackTrace();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska!! Predmet nije snimljen"));
            }
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Morate odabrati katedru"));
        }
    }

    public void azirirajPredmet() {
        if (imaKatedra) {
            Session session = Util.getSesion();
            Transaction t = session.beginTransaction();
            try {
                selectedPredmet.setKatedra(selectedKatedra);
                session.update(selectedPredmet);
                t.commit();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Predmet snimljen"));
            } catch (Exception e) {
                t.rollback();
                e.printStackTrace();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska!! Predmet nije snimljen"));
            }
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Morate odabrati katedru"));
        }

    }

    private void loadPredmeti() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria c = session.createCriteria(Predmet.class);
            predmetList = (ArrayList<Predmet>) c.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
        }
    }

    public void onChangePredmet() {
        selectedPredmet = new Predmet();
        imaPredmet = false;
        if (!"".equals(selectedPredmetString)) {
            for (Predmet p : predmetList) {
                if (selectedPredmetString.equals(p.getNaziv())) {
                    selectedPredmet = p;
                    imaPredmet = true;
                    break;
                }
            }
        }
    }

    public ArrayList<Predmet> getPredmetList() {
        return predmetList;
    }

    public void setPredmetList(ArrayList<Predmet> predmetList) {
        this.predmetList = predmetList;
    }

    public String getSelectedPredmetString() {
        return selectedPredmetString;
    }

    public void setSelectedPredmetString(String selectedPredmetString) {
        this.selectedPredmetString = selectedPredmetString;
    }

    public Predmet getSelectedPredmet() {
        return selectedPredmet;
    }

    public void setSelectedPredmet(Predmet selectedPredmet) {
        this.selectedPredmet = selectedPredmet;
    }

    public boolean isImaPredmet() {
        return imaPredmet;
    }

    public void setImaPredmet(boolean imaPredmet) {
        this.imaPredmet = imaPredmet;
    }

    public Predmet getNoviPredmet() {
        return noviPredmet;
    }

    public void setNoviPredmet(Predmet noviPredmet) {
        this.noviPredmet = noviPredmet;
    }

    public ArrayList<Katedra> getKatedreList() {
        return katedreList;
    }

    public void setKatedreList(ArrayList<Katedra> katedreList) {
        this.katedreList = katedreList;
    }

    public String getSelectedKatedraString() {
        return selectedKatedraString;
    }

    public void setSelectedKatedraString(String selectedKatedraString) {
        this.selectedKatedraString = selectedKatedraString;
    }

    public Katedra getSelectedKatedra() {
        return selectedKatedra;
    }

    public void setSelectedKatedra(Katedra selectedKatedra) {
        this.selectedKatedra = selectedKatedra;
    }

    public boolean isImaKatedra() {
        return imaKatedra;
    }

    public void setImaKatedra(boolean imaKatedra) {
        this.imaKatedra = imaKatedra;
    }

}
